using Maticsoft.DBUtility;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace uTrade.Core
{
    public class BackTestStragCfgMgr
    {
        public BackTestStragCfgMgr()
        { }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public static int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("Name", "tbl_bt_stragcfg");
        }

        /// <summary>
        /// 清空
        /// </summary>
        public static int Clear()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_bt_stragcfg ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            return rows;
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbl_bt_stragcfg");
            strSql.Append(" where Name=@name");
            SqlParameter[] parameters = {
                    new SqlParameter("@name", SqlDbType.VarChar ,40)
            };
            parameters[0].Value = name;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static int Add(StrategyConfig model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbl_bt_stragcfg(");
            strSql.Append("Name, StragName, Instrument, InstrumentOrder, Interval, BeginDate, EndDate, Status, Params)");
            strSql.Append(" values (");
            strSql.Append("@Name, @StragName, @Instrument, @InstrumentOrder, @Interval, @BeginDate, @EndDate, @Status, @Params)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@Name", SqlDbType.VarChar ,40),
new SqlParameter("@StragName", SqlDbType.VarChar ,64),
new SqlParameter("@Instrument", SqlDbType.VarChar ,20),
new SqlParameter("@InstrumentOrder", SqlDbType.VarChar ,20),
new SqlParameter("@Interval", SqlDbType.VarChar ,20),
new SqlParameter("@BeginDate", SqlDbType.DateTime),
new SqlParameter("@EndDate", SqlDbType.DateTime),
new SqlParameter("@Status", SqlDbType.VarChar ,10),
new SqlParameter("@Params", SqlDbType.VarChar ,64)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.StragName;
            parameters[2].Value = model.Instrument;
            parameters[3].Value = model.InstrumentOrder;
            parameters[4].Value = model.Interval;
            parameters[5].Value = model.BeginDate;
            parameters[6].Value = model.EndDate;
            parameters[7].Value = model.Status;
            parameters[8].Value = model.Params;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static int Update(StrategyConfig model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbl_bt_stragcfg set ");
            strSql.Append("StragName = @StragName,");
            strSql.Append("Instrument = @Instrument,");
            strSql.Append("InstrumentOrder = @InstrumentOrder,");
            strSql.Append("Interval = @Interval,");
            strSql.Append("BeginDate = @BeginDate,");
            strSql.Append("EndDate = @EndDate,");
            strSql.Append("Status = @Status,");
            strSql.Append("Params = @Params,");

            strSql.Append(" where Name=@name");
            SqlParameter[] parameters = {
                    new SqlParameter("@Name", SqlDbType.VarChar ,40),
new SqlParameter("@StragName", SqlDbType.VarChar ,64),
new SqlParameter("@Instrument", SqlDbType.VarChar ,20),
new SqlParameter("@InstrumentOrder", SqlDbType.VarChar ,20),
new SqlParameter("@Interval", SqlDbType.VarChar ,20),
new SqlParameter("@BeginDate", SqlDbType.DateTime),
new SqlParameter("@EndDate", SqlDbType.DateTime),
new SqlParameter("@Status", SqlDbType.VarChar ,10),
new SqlParameter("@Params", SqlDbType.VarChar ,64)};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.StragName;
            parameters[2].Value = model.Instrument;
            parameters[3].Value = model.InstrumentOrder;
            parameters[4].Value = model.Interval;
            parameters[5].Value = model.BeginDate;
            parameters[6].Value = model.EndDate;
            parameters[7].Value = model.Status;
            parameters[8].Value = model.Params;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static bool Delete(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_bt_stragcfg ");
            strSql.Append(" where Name=@name");
            SqlParameter[] parameters = {
                    new SqlParameter("@name", SqlDbType.VarChar ,40)
            };
            parameters[0].Value = name;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 批量删除数据
        /// </summary>
        public static int DeleteList(string namelist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbl_bt_stragcfg ");
            strSql.Append(" where Name in (" + namelist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            return rows;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static StrategyConfig GetModel(string name)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Name, StragName, Instrument, InstrumentOrder, Interval, BeginDate, EndDate, Status, Params from tbl_bt_stragcfg ");
            strSql.Append(" where Name=@name");
            SqlParameter[] parameters = {
                    new SqlParameter("@name", SqlDbType.VarChar ,40)
            };
            parameters[0].Value = name;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ModelConvertHelper<StrategyConfig>.ConvertToModel(ds.Tables[0])?[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Name, StragName, Instrument, InstrumentOrder, Interval, BeginDate, EndDate, Status, Params ");
            strSql.Append(" FROM tbl_bt_stragcfg ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public static DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" Name, StragName, Instrument, InstrumentOrder, Interval, BeginDate, EndDate, Status, Params ");
            strSql.Append(" FROM tbl_bt_stragcfg ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public static int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbl_bt_stragcfg ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.Name desc");
            }
            strSql.Append(")AS Row, T.*  from tbl_bt_stragcfg T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbl_bt_stragcfg";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/
    }
}